package com.tnt.ocr.aliyun;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author zhuangxl
 * @description
 * @since 2021-02-05 8:44
 */
public class Test {

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        String imgFile = "F:\\1-1.png";
        DemoIDCardOcr.iDCardOcrHttpSyncTest(imgFile);
        System.out.println("耗时："+ (System.currentTimeMillis() - start)/1000.0f);
    }

    public String getBodyString(String  imgFile){

        //请根据线上文档修改configure字段
        JSONObject configObj = new JSONObject();
        configObj.put("side", "face");
        String config_str = configObj.toString();
        //            configObj.put("min_size", 5);
        //            String config_str = "";

        // 对图像进行base64编码
        String imgBase64 = "";
        try {
            if(imgFile.startsWith("http")){
                imgBase64 = imgFile;
            }else {
                File file = new File(imgFile);
                byte[] content = new byte[(int) file.length()];
                FileInputStream finputstream = new FileInputStream(file);
                finputstream.read(content);
                finputstream.close();
                imgBase64 = new String(Base64.encodeBase64(content));
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        // 拼装请求body的json字符串
        JSONObject requestObj = new JSONObject();
        try {
            requestObj.put("image", imgBase64);
            if(config_str.length() > 0) {
                requestObj.put("configure", config_str);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //Body内容
        String body = requestObj.toString();



        return body;
    }
}
